package com.spring.springbootswaggerstudy.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.spring.springbootswaggerstudy.controller.HelloController;
import com.spring.springbootswaggerstudy.controller.HelloWorldController2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
@EnableKnife4j
public class SwaggerConfig {

    //多个Docket实现多组
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A")
                .select()
                .apis(RequestHandlerSelectors.none())
                .build();
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B")
                .select()
                .apis(RequestHandlerSelectors.none())
                .build();
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C")
                .select()
                .apis(RequestHandlerSelectors.none())
                .build();
    }



    //配置了Swagger的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger环境
        Profiles prfiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles判断是否出处在自己设定的环境当中
        boolean flag=environment.acceptsProfiles(prfiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("D")
                .enable(true)//enable是否启动Swagger，如果为false不启动，则不能在浏览器里访问Swagger如果为true启动Swagger
                .select()// 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                //RequestHandlerSelectors 扫描要配置接口的方式
                //basePackage 指定要扫描的包,根据包路径扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.spring.springbootswaggerstudy.controller"))
                //any():扫描全部,项目中的所有接口都会被扫描到
                //none():全不扫描
                //withClassAnnotation(HelloController.class):扫描类上的注解
                //withMethodAnnotation(:扫描方法上的注解,扫描，如withMethodAnnotation(GetMapping.class)只扫描get请求
                //.apis(RequestHandlerSelectors.any())
                //paths():过滤路径,配置如何通过path过滤,即这里只扫描请求以/controller/开头的接口
                // .paths(PathSelectors.ant("/controller/**"))
                .build();
    }
    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息：参数一：作者姓名，参数二：作者访问链接，参数三：作者邮箱
        Contact contact=new Contact("D","http://www.apache.org/licenses/LICENSE-2.0","D的邮箱");
        return new ApiInfo("D的SwaggerAPI文档",//标题
                "swagger学习",//描述
                "v1.0",//版本
                "http://www.apache.org/licenses/LICENSE-2.0",//组织链接
                contact,//作者信息
                "Apache 2.0",//许可
                "http://www.apache.org/licenses/LICENSE-2.0",//许可链接
                new ArrayList()//扩展
        );
    }
}